<template>
  <a-modal
    width="800px"
    title="合作配置"
    :visible="visible"
    okText="保存"
    @ok="handleOk"
    @cancel="handleCancel"
    :confirmLoading="confirmLoading"
  >
    <a-form-model
      ref="form"
      :model="form"
      :rules="rules"
      layout="horizontal"
      :labelCol="{ span: 4 }"
      :wrapperCol="{ span: 20 }"
    >
      <a-form-model-item label="报名名额:" prop="registerLimit">
        <div class="disFlxAC min-height-4">
          <a-radio-group
            class="flexS0"
            v-model="form.registerLimit"
            :options="Dict.getLocalMap('EnrollmentLimitTypeEnumDict', 'label', 'value')"
          ></a-radio-group>
          <div v-if="form.registerLimit === EnrollmentLimitTypeEnum.Limited" class="disFlxAC">
            ,最多
            <a-form-model-item prop="registerLimitNum" style="margin-bottom:0">
              <a-input class="_mr-10" style="width:120px" v-model="form.registerLimitNum">
                <span slot="addonAfter">人</span>
              </a-input>
            </a-form-model-item>
          </div>
        </div>
      </a-form-model-item>
      <!-- <a-form-model-item
        label="报名通知短信:"
        prop="registerSmsType"
        extra="报名审核结果通知将根据本设置使用相应组织的短信签名发送"
      >
        <div class="disFlxAC min-height-4">
          <a-radio-group
            class="flexS0"
            v-model="form.registerSmsType"
            :options="Dict.getLocalMap('EnrollmentNotificationMethodEnumDict', 'label', 'value')"
          ></a-radio-group>
        </div>
      </a-form-model-item> -->
    </a-form-model>
  </a-modal>
</template>

<script>
import SSwitch from '@/components/Switch'
import { EnrollmentLimitTypeEnum, EnrollmentNotificationMethodEnum } from '@/enums/activity.js'
import activity from '@/utils/dict/dictItem/activity'
import { positiveIntegerRegex } from '@/utils/RegExp'

import { validMaxNum } from '@/api/activity/activity'
const initForm = {
  registerLimit: EnrollmentLimitTypeEnum.Unlimited,
  registerSmsType: EnrollmentNotificationMethodEnum.UnifiedBroadcast,
  registerLimitNum: undefined
}
export default {
  name: 'CooperationConfig',
  components: {
    SSwitch
  },
  data() {
    return {
      EnrollmentLimitTypeEnum,
      visible: false,
      confirmLoading: false,
      form: { ...initForm },
      rules: {
        registerLimitNum: [
          {
            validator: (rule, value, callback) => {
              if (!value) {
                callback(new Error('请输入报名人数'))
              }
              if (!positiveIntegerRegex.test(value)) callback(new Error('请输入大于0的整数'))
              callback()
            },
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    moduleShow(info) {
      this.form = {
        id: info.id,
        tenantId: info.tenantId,
        registerLimit: info.registerLimit || EnrollmentLimitTypeEnum.Unlimited,
        registerSmsType: info.registerSmsType || EnrollmentNotificationMethodEnum.UnifiedBroadcast,
        registerLimitNum: info.registerLimitNum || undefined
      }
      this.visible = true
    },
    handleOk() {
      try {
        this.confirmLoading = true
        this.$refs.form.validate(async valid => {
          if (valid) {
            const params = { ...this.form, activityId: this.$route.query.activityId }
            const res = await validMaxNum(params)
            if (res.code) return
            this.visible = false
            this.$emit('ok', this.form)
            this.handleCancel()
          }
        })
      } catch (error) {
      } finally {
        this.confirmLoading = false
      }
    },
    handleCancel() {
      this.form = { ...initForm }
      this.visible = false
    }
  }
}
</script>
